<!--********************************************************************
* Copyright© 2000 - 2025 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title data-i18n="resources.title_serviceAreas"></title>
    <style>
      #toolbar {
        position: absolute;
        top: 50px;
        right: 10px;
        text-align: center;
        z-index: 1000;
        border-radius: 4px;
      }
    </style>
</head>
<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%; position: absolute;top: 0;">
<div id="toolbar" class="panel panel-primary">
  <div class='panel-heading'>
      <h5 class='panel-title text-center' data-i18n="resources.text_serviceAreas"></h5>
  </div>
  <div class='panel-body content'>
      <input type="button" class="btn btn-default" data-i18n="[value]resources.text_serviceCenter"
              onclick="selectCenters()"/>&nbsp;
      <input type="button" class="btn btn-default" data-i18n="[value]resources.text_input_value_submit"
              onclick="findServiceAreas()"/>&nbsp;
      <input type="button" class="btn btn-default" data-i18n="[value]resources.text_input_value_clear"
              onclick="clearElements(true)"/>
  </div>
</div>
<div id="map" style="width: 100%;height:100%"></div>
<script type="text/javascript" include="bootstrap,widgets.alert" src="../js/include-web.js"></script>
<script type="text/javascript" src="../../dist/ol/include-ol.js"></script>
<script type="text/javascript">
    var map, source, draw, centersArray = [], weightsArray = [], resultLayerArr = [],
        baseUrl = (window.isLocal ? window.server : "https://iserver.supermap.io")+"/iserver/services/map-changchun/rest/maps/长春市区图",
        serviceUrl = (window.isLocal ? window.server : "https://iserver.supermap.io")+"/iserver/services/transportationanalyst-sample/rest/networkanalyst/RoadNet@Changchun";
    var extent = [48.4, -7668.25, 8958.85, -55.58];
    var projection = new ol.proj.Projection({
        code:'',
        extent: extent,
        units: 'm'
    });
    new ol.supermap.MapService(baseUrl).getMapInfo(function (serviceResult) {
        var mapJSONObj = serviceResult.result;
        map = new ol.Map({
            target: 'map',
            // ol v7版本用法为ol.control.defaults.defaults； v6版本以下用法为ol.control.defaults
            controls: ol.control.defaults.defaults({attributionOptions: {collapsed: false}})
                .extend([new ol.supermap.control.Logo({ link: "https://iclient.supermap.io" })]),
            view: new ol.View({
                center: [5105, -3375],
                zoom: 4,
                projection: projection,
                multiWorld: true
            })
        });
        var layer = new ol.layer.Tile({
            source: new ol.source.TileSuperMapRest(ol.source.TileSuperMapRest.optionsFromMapJSON(baseUrl, mapJSONObj))
        });
        source = new ol.source.Vector();
        var vectorLayer = new ol.layer.Vector({
            source: source
        });
        map.addLayer(layer);
        map.addLayer(vectorLayer);
    });

    function selectCenters() { 
      clearElements(true);
      draw = new ol.interaction.Draw({
        source: source,
        type: 'Point'
      });
      draw.on('drawend', function(e) {
        var feature = e.feature;
        if (feature) {
          // 设置点的样式
          var iconStyle = new ol.style.Style({
            image: new ol.style.Icon({
              src: '../img/marker-icon.png'
            }),
          });
          feature.setStyle(iconStyle);
          centersArray.push(feature.getGeometry());
          weightsArray.push([400 + Math.random() * 100]);
        }
      });
      map.addInteraction(draw);
    }

    function findServiceAreas() {
        clearElements();
        if (!centersArray.length) {
          return widgets.alert.showAlert(resources.msg_centerPoint, false);
        }
        var resultSetting = new ol.supermap.TransportationAnalystResultSetting({
            returnEdgeFeatures: true,
            returnEdgeGeometry: true,
            returnEdgeIDs: true,
            returnNodeFeatures: true,
            returnNodeGeometry: true,
            returnNodeIDs: true,
            returnPathGuides: true,
            returnRoutes: true
        });
        var analystParameter = new ol.supermap.TransportationAnalystParameter({
            resultSetting: resultSetting,
            weightFieldName: "length"
        });
        var parameter = new ol.supermap.FindServiceAreasParameters({
            centers: centersArray,
            isAnalyzeById: false,
            parameter: analystParameter
        });
        parameter.weights = weightsArray;

        // new ol.supermap.NetworkAnalystService(serviceUrl).findServiceAreas(parameter, function (serviceResult) {
        //     var vectorSource = new ol.source.Vector({
        //         features: (new ol.format.GeoJSON()).readFeatures(serviceResult.result.serviceAreaList[0].edgeFeatures)
        //     });
        //     var resultLayer = new ol.layer.Vector({
        //         source: vectorSource
        //     });
        //     map.addLayer(resultLayer);
        // });

        new ol.supermap.NetworkAnalystService(serviceUrl).findServiceAreas(parameter).then(function (serviceResult) {
          var result = serviceResult.result;
          result && result.serviceAreaList.map(function (serviceArea) {
              var vectorSource = new ol.source.Vector({
                features: (new ol.format.GeoJSON()).readFeatures(serviceArea.serviceRegion)
              });
              var resultLayer = new ol.layer.Vector({
                source: vectorSource
              });
              map.addLayer(resultLayer);
              resultLayerArr.push(resultLayer);
          });
        });
    }

    function clearElements(clearPoint) {
        draw && map.removeInteraction(draw);
        resultLayerArr.forEach(function(layer) {
          map.removeLayer(layer);
        });
        if (clearPoint) {
          centersArray = [];
          weightsArray = [];
          resultLayerArr = [];
          source.clear();
        }

    }
</script>
</body>
</html>